<template>
	<div class="app-container">
		<el-form class="queryForms" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="记录编号" prop="recordNumber">
				<el-input v-model="queryParams.recordNumber" placeholder="请输入记录编号" clearable maxlength="20"
					show-word-limit @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="编号" prop="serialNumber">
				<el-input v-model="queryParams.serialNumber" placeholder="请输入编号" clearable maxlength="20"
					show-word-limit @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<CreateForm :ids="ids" formtype="t_exportProperty" />

			<right-toolbar @changeTableData="changeTableData" menuNames="propertyRegistrationRecord77"
				:showSearch.sync="showSearch" @queryTable="handleQuery" :columns.sync="columns"
				perm="erp:propertyRegistrationRecord:list"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''"
			class="informationTab" ref="eleTab">
			<el-table ref="table" v-loading="loading" :data="propertyRegistrationRecordList"
				@selection-change="handleSelectionChange" row-key="id" lazy :load="loadListDes"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55">
					           <template slot-scope="scope">

						<div>{{ scope.row.sort }}</div>
						           
					</template>

				</el-table-column>

				<el-table-column label="记录编号" prop="recordNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[0].visible" />
				<el-table-column label="编号" prop="serialNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[1].visible" />
				<el-table-column label="日期" min-width="150" :show-overflow-tooltip="true" prop="outsourcingDate"
					v-if="columns[2].visible" />
				<el-table-column prop="deviceTypeId" label="顾客名称" min-width="150" :show-overflow-tooltip="true"
					align="center" v-if="columns[3].visible">
					<template slot-scope="scope">
						<el-select v-model="scope.row.deviceTypeId" @change="selectdeviceType(scope.row)"
							v-if="!scope.row.hasChildren">
							<el-option v-for="dict in dict.type.default_custom_name" :key="dict.value"
								:label="dict.label" :value="dict.value"></el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="生产批次" prop="productionBatch" min-width="150" :show-overflow-tooltip="true"
					align="center" v-if="columns[4].visible" />
				<el-table-column label="物料名称" prop="partName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[5].visible" />
				<el-table-column label="物料编码" prop="partFigure" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[6].visible" />
				<el-table-column label="数量" align="center" prop="quantityquantity" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[7].visible" />
				<el-table-column label="发放人" align="center" prop="issuerName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[8].visible" />
				<el-table-column label="发放日期" align="center" prop="issuerDate" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="合格证接收人" align="center" prop="certificateRecipientName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[10].visible" />
				<el-table-column label="接收日期" align="center" prop="certificateRecipientDate" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[11].visible" />
				<el-table-column label="零件接收人" align="center" prop="partRecipientName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[12].visible" />
				<el-table-column label="接收日期" align="center" prop="partRecipientDate" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[13].visible" />
				<el-table-column label="状态" align="center" prop="outerApprovalStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[14].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.outerApprovalStatus == '0'">未提交</div>
						<div v-if="scope.row.outerApprovalStatus == '1'">审批中</div>
						<div v-if="scope.row.outerApprovalStatus == '2'">已通过</div>
						<div v-if="scope.row.outerApprovalStatus == '3'">未通过</div>
					</template>
				</el-table-column>
				<el-table-column label="设备状态" align="center" prop="registrationStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[15].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.registrationStatus == '0'">未提交</div>
						<div v-if="scope.row.registrationStatus == '1'">已提交</div>
						<div v-if="scope.row.registrationStatus == '2'">外委中</div>
						<div v-if="scope.row.registrationStatus == '3'">合格证已上传</div>
						<div v-if="scope.row.registrationStatus == '4'">待接收</div>
						<div v-if="scope.row.registrationStatus == '5'">已接收</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
					width="200">
					<template slot-scope="scope">
						<el-button
							v-if="(scope.row.outerApprovalStatus == '0' && scope.row.hasChildren) || (scope.row.outerApprovalStatus == '3' && scope.row.hasChildren)"
							size="mini" type="text" icon="el-icon-circle-check" @click="handleSubmit(scope.row)"
							v-hasPermi="['erp:propertyRegistrationRecord:submit']">提交</el-button>
						<el-button size="mini" type="text" icon="el-icon-s-order" @click="handleApprove(scope.row)"
							v-hasPermi="['erp:propertyRegistrationRecord:records']"
							v-if="scope.row.hasChildren && scope.row.existApprovalStatus == '0'">审核</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleAccept(scope.row)"
							v-hasPermi="['erp:propertyRegistrationRecord:receive']"
							v-if="!scope.row.hasChildren && scope.row.registrationStatus == '4'">接收</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
							v-hasPermi="['erp:propertyRegistrationRecord:delete']"
							v-if="scope.row.hasChildren">删除</el-button>
						<el-button size="mini" type="text" icon="el-icon-folder" @click="handleFile(scope.row)"
							v-hasPermi="['erp:propertyRegistrationRecord:file']"
							v-if="!scope.row.hasChildren">附件</el-button>
					</template>
				</el-table-column>
			</el-table>

			<pagination menuNames="Pagination80" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
				:limit.sync="queryParams.pageSize" @pagination="getList" />
		</div>
		<!-- 添加或修改顾客财产登记(外委)记录对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="记录编号" prop="recordNumber">
					<el-input v-model="form.recordNumber" placeholder="请输入记录编号" />
				</el-form-item>
				<el-form-item label="编号" prop="serialNumber">
					<el-input v-model="form.serialNumber" placeholder="请输入编号" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注" />
				</el-form-item>
				<el-form-item label="删除标志" prop="delFlag">
					<el-input v-model="form.delFlag" placeholder="请输入删除标志" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="审批" :visible.sync="openApprove" width="900px" append-to-body v-dialogDrag>
			<ApproveTemplete @cancelApprove="cancelApprove" :openApprove="openApprove"></ApproveTemplete>
		</el-dialog>
	</div>
</template>

<script>
import searchStateMixin from '@/layout/mixin/searchStateMixin.js'   
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
import CreateForm from "@/views/components/CreateForm"
import ApproveTemplete from "@/views/components/ApproveTemplete"
import { receive, approverRepair, submitRegistrationRecord, listPropertyRegistrationRecord, getPropertyRegistrationRecord, delPropertyRegistrationRecord, addPropertyRegistrationRecord, updatePropertyRegistrationRecord } from "@/api/taskFilling/propertyRegistrationRecord"
import { listPropertyRegistrationOuter, updatePropertyRegistrationOuter } from "@/api/taskFilling/propertyRegistrationOuter"
export default {
	mixins: [searchStateMixin], 
	name: "PropertyRegistrationRecord",
	components: { ApproveTemplete, CreateForm },
	dicts: ["default_custom_name"],
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			eleTabTop: 0, //表格容器距浏览器顶部距离
			treetablekey: Math.random(),
			// 审批数据
			approveForm: {},
			openApprove: false,
			// 更新树刷新
			treeMap: new Map(),
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 顾客财产登记(外委)记录表格数据
			propertyRegistrationRecordList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				recordNumber: null,
				serialNumber: null,
				recordStatus: null,
				outerApprovalStatus: null
			},
			// 表单参数
			form: {},
			approveForm: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `记录编号`, visible: true },
				{ key: 1, label: `编号`, visible: true },
				{ key: 2, label: `日期`, visible: true },
				{ key: 3, label: `顾客名称`, visible: true },
				{ key: 4, label: `生产批次`, visible: true },
				{ key: 5, label: `物料名称`, visible: true },
				{ key: 6, label: `物料编码`, visible: true },
				{ key: 7, label: `数量`, visible: true },
				{ key: 8, label: `发放人`, visible: true },
				{ key: 9, label: `发放日期`, visible: true },
				{ key: 10, label: `合格证接收人`, visible: true },
				{ key: 11, label: `接收日期`, visible: true },
				{ key: 12, label: `零件接收人`, visible: true },
				{ key: 13, label: `接收日期`, visible: true },
				{ key: 14, label: `状态`, visible: true },
				{ key: 15, label: `设备状态`, visible: true }
			]
		}
	},
	created() {
		// this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	watch: {
		"$route.query": {
			handler(newQuery, oldQuery) {
				if (newQuery.queryData) {
					if (Object.keys(newQuery.queryData).length === 0) {
						this.resetForm("queryForm")
						// this.getSize()
					} else {
						this.queryParams = { ...this.queryParams, ...JSON.parse(newQuery.queryData) }
						// this.getSize()
					}
				} this.getSize()
			},
			immediate: true, // 立即执行一次
			deep: true // 深度监听
		}
	},
	activated() {
		// this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination80" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		selectdeviceType(row) {
			row.id = row.hasChildren ? row.id : row.id.substring(3)
			updatePropertyRegistrationOuter(row).then(res => {
				this.$modal.msgSuccess("设置成功")
				this.getList()
			})
		},
		handleSubmit(row) {
			let params = {
				submitStatus: row.outerApprovalStatus == "3" ? "2" : "1",
				workId: row.id,
				workflowType: "7"
			}
			this.$modal
				.confirm(`是否确认提交数据？`)
				.then(function () {
					return submitRegistrationRecord(params)
				})
				.then(() => {
					this.getList()
					localStorage.setItem("todoMessage", "RegistrationRecord1")
					_this.$modal.msgSuccess("提交成功")
				})
				.catch(() => { })
		},
		handleApprove(row) {
			this.approveForm = {
				id: row.hasChildren ? row.id : row.id.substring(3),
				status: row.hasChildren ? true : false
			}
			this.openApprove = true
		},
		cancelApprove(data) {
			if (data.flag) {
				const _this = this
				let params = {
					id: _this.approveForm.id,
					approvalStatus: data.approvalStatus,
					causeRejection: data.causeRejection,
					workflowType: "7"
				}
				this.$modal
					.confirm(`是否确认审批数据？`)
					.then(function () {
						return approverRepair(params)
					})
					.then(() => {
						localStorage.setItem("todoMessage", "RegistrationRecord2")
						this.getList()
						_this.$modal.msgSuccess("审批成功")
					})
					.catch(() => { })
			}
			this.openApprove = false
		},
		handleAccept(row) {
			this.$modal
				.confirm(`是否确认接收数据？`)
				.then(function () {
					return receive([row.id.substring(3)])
				})
				.then(() => {
					localStorage.setItem("todoMessage", "RegistrationRecord3")
					this.getList()
					_this.$modal.msgSuccess("接收成功")
				})
				.catch(() => { })
		},
		handleFile(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id.substring(3),
					typeId: 20,
					perm: "erp:propertyRegistrationRecord:file"
				}
			})
		},

		// 获取列表子菜单
		async loadListDes(tree, treeNode, resolve) {
			if (!this.treeMap.has(tree.id)) {
				this.treeMap.set(tree.id, {
					treeNode,
					resolve
				})
			}
			let ret = await listPropertyRegistrationOuter({ propertyRegistrationRecordId: tree.id })
			ret.rows.forEach((item, index) => {
				// 父子id会冲突
				item.id = "son" + item.id
				item.sort = index + 1
			})
			resolve(ret.rows)
		},
		/** 查询顾客财产登记(外委)记录列表 */
		getList() {
			this.loading = true
			listPropertyRegistrationRecord(this.queryParams).then(response => {
				this.propertyRegistrationRecordList = response.rows
				this.propertyRegistrationRecordList.forEach((item, index) => {
					item.hasChildren = true
					item.sort = index + 1
					this.$refs.table.toggleRowExpansion(item, false)
				})
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				recordNumber: null,
				serialNumber: null,
				recordStatus: null,
				outerApprovalStatus: null,
				remark: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.treetablekey = Math.random()
			this.queryParams.pageNum = 1
			this.saveSearchState()   
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.queryParams.recordStatus = undefined
			this.queryParams.outerApprovalStatus = undefined
			this.clearSearchCache()
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加顾客财产登记(外委)记录"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getPropertyRegistrationRecord(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改顾客财产登记(外委)记录"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updatePropertyRegistrationRecord(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addPropertyRegistrationRecord(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除顾客财产登记(外委)记录数据项？")
				.then(function () {
					return delPropertyRegistrationRecord(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => { })
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/propertyRegistrationRecord/export",
				{
					...this.queryParams
				},
				`propertyRegistrationRecord_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
